package com.vibeplayer.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.vibeplayer.app.R;
import com.vibeplayer.app.fragment.LyricEditFragment;
import com.vibeplayer.app.fragment.LyricPreviewFragment;
import com.vibeplayer.app.model.Song;

public class LyricEditorActivity extends AppCompatActivity {

    public static final String EXTRA_SONG = "extra_song";
    
    private ViewPager2 viewPager;
    private TabLayout tabLayout;
    private TextView txtSongInfo;
    private Song song;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lyric_editor);
        
        // 获取传入的歌曲信息
        Intent intent = getIntent();
        if (intent != null && intent.hasExtra(EXTRA_SONG)) {
            song = intent.getParcelableExtra(EXTRA_SONG);
        }
        
        if (song == null) {
            finish();
            return;
        }
        
        // 初始化视图
        initViews();
        
        // 设置歌曲信息
        String songInfo = song.getTitle() + " - " + song.getArtist();
        txtSongInfo.setText(songInfo);
    }
    
    private void initViews() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        
        txtSongInfo = findViewById(R.id.txtSongInfo);
        viewPager = findViewById(R.id.viewPager);
        tabLayout = findViewById(R.id.tabLayout);
        
        // 设置ViewPager适配器
        viewPager.setAdapter(new LyricPagerAdapter(this));
        
        // 连接TabLayout和ViewPager
        new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
            switch (position) {
                case 0:
                    tab.setText(R.string.edit_lyric);
                    break;
                case 1:
                    tab.setText(R.string.preview_lyric);
                    break;
            }
        }).attach();
    }
    
    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }
    
    /**
     * 获取当前歌曲
     * @return 当前歌曲
     */
    public Song getSong() {
        return song;
    }
    
    /**
     * ViewPager适配器
     */
    private class LyricPagerAdapter extends FragmentStateAdapter {
        
        public LyricPagerAdapter(@NonNull FragmentActivity fragmentActivity) {
            super(fragmentActivity);
        }
        
        @NonNull
        @Override
        public Fragment createFragment(int position) {
            switch (position) {
                case 0:
                    return new LyricEditFragment();
                case 1:
                    return new LyricPreviewFragment();
                default:
                    return new LyricEditFragment();
            }
        }
        
        @Override
        public int getItemCount() {
            return 2;
        }
    }
} 